package org.tis.tools.abf.module.ac.controller;

import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorBatchAddDeleteRequest;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorFuncDateRequest;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorFuncQueConditionRequest;
import org.tis.tools.abf.module.ac.controller.request.AcOperatorFuncRequest;
import org.tis.tools.abf.module.ac.entity.AcOperatorFunc;
import org.tis.tools.abf.module.ac.service.IAcOperatorFuncService;
import org.tis.tools.core.web.controller.BaseController;
import org.tis.tools.core.web.vo.SmartPage;
import org.tis.tools.model.common.ResultVO;
import org.tis.tools.model.log.enums.OperateType;
import org.tis.tools.starter.operatelog.annotation.OperateLog;

import javax.validation.constraints.NotBlank;

/**
 * acOperatorFunc的Controller类
 * 操作员特殊权限配置
 * @author Auto Generate Tools
 * @date 2018/04/23
 */
@RestController
@RequestMapping("/acOperatorFunc")
@Slf4j
public class AcOperatorFuncController extends BaseController<AcOperatorFunc>  {

    @Autowired
    private IAcOperatorFuncService acOperatorFuncService;

    @ApiOperation(value = "新增操作员特殊权限配置",notes = "传入AcOperatorFuncRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "新增操作员特殊权限配置")
    @PostMapping
    public ResultVO add(@RequestBody @Validated @ApiParam(value = "AcOperatorFuncRequest",name = "传入的AcOperatorFuncRequest类",required = true) AcOperatorFuncRequest acOperatorFuncRequest) {
        acOperatorFuncService.add(acOperatorFuncRequest);
        return ResultVO.success("新增成功！");
    }

    @ApiOperation(value = "修改操作员特殊权限配置",notes = "传入AcOperatorFuncRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改操作员特殊权限配置")
    @PutMapping
    public ResultVO update(@RequestBody @Validated @ApiParam(value = "AcOperatorFuncRequest",name = "传入的AcOperatorFuncRequest类",required = true) AcOperatorFuncRequest acOperatorFuncRequestc) {
        AcOperatorFunc acOperatorFunc = acOperatorFuncService.selectById(acOperatorFuncRequestc.getGuid());
        if (acOperatorFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acOperatorFunc = acOperatorFuncService.change(acOperatorFuncRequestc);
        return ResultVO.success("修改成功！",acOperatorFunc);
    }


    @ApiOperation(value = "根据GUID删除操作员特殊权限配置",notes = "传入操作员特殊权限配置的GUID")
    @DeleteMapping("/{id}")
    public ResultVO delete(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "操作员特殊权限配置的GUID",required = true) String id) {
        AcOperatorFunc acOperatorFunc = acOperatorFuncService.selectById(id);
        if (acOperatorFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        acOperatorFuncService.deleteOperatorFunc(id);
        return ResultVO.success("删除成功");
    }


    @ApiOperation(value = "根据GUID查询操作员特殊权限配置",notes = "传入操作员特殊权限配置的GUID")
    @GetMapping("/{id}")
    public ResultVO detail(@PathVariable @NotBlank(message = "id不能为空") @ApiParam(value = "id",name = "操作员特殊权限配置的GUID",required = true) String id) {
        AcOperatorFunc acOperatorFunc = acOperatorFuncService.selectById(id);
        if (acOperatorFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }
        return ResultVO.success("查询成功", acOperatorFunc);
    }


    @ApiOperation(value = "分页查询操作员特殊权限配置",notes = "传入分页信息作为参数")
    @PostMapping("/list")
    public ResultVO list(@RequestBody @Validated @ApiParam(value = "page",name = "需要传入的分页对象",required = true) SmartPage<AcOperatorFunc> page) {
        return  ResultVO.success("查询成功", acOperatorFuncService.selectPage(page.getPageObj(), page.getConditionWrapper()));
    }


    @ApiOperation(value = "修改有效时间",notes = "传入AcOperatorFuncDateRequest类作为参数")
    @OperateLog(type = OperateType.UPDATE,desc = "修改有效时间")
    @PutMapping("/setDate")
    public ResultVO setDate(@RequestBody @Validated @ApiParam(value = "AcOperatorFuncDateRequest",name = "需要传入的AcOperatorFuncDateRequest类",required = true) AcOperatorFuncDateRequest acOperatorFuncDateRequest){

        AcOperatorFunc acOperatorFunc = acOperatorFuncService.selectById(acOperatorFuncDateRequest.getGuid());
        if (acOperatorFunc == null) {
            return ResultVO.error("404", "找不到对应记录或已经被删除！");
        }

        //收集参数
        acOperatorFunc.setStartDate(acOperatorFuncDateRequest.getStartDate());
        acOperatorFunc.setEndDate(acOperatorFuncDateRequest.getEndDate());

        acOperatorFuncService.updateById(acOperatorFunc);

        return ResultVO.success("时间设置成功",acOperatorFunc);
    }


    @ApiOperation(value = "查询所有应用和操作员下已有应用",notes = "传入操作员GUID作为参数")
    @GetMapping("/queryAppByOperator/{operatorId}")
    public ResultVO queryAppByOperator (@PathVariable @NotBlank(message = "操作员id不能为空") @ApiParam(value = "operatorId",name = "操作员GUID",required = true) String operatorId){
        return ResultVO.success("查询成功",acOperatorFuncService.queryAppByOperator(operatorId));
    }


    @ApiOperation(value = "查询应用和操作员下已有的功能和行为和所有的功能行为",notes = "传入AcOperatorFuncQueConditionRequest类作为参数")
    @PostMapping("/queryFuncBehaveByOperatorApp")
    public ResultVO queryFuncBehaveByOperatorApp (@RequestBody @Validated @ApiParam(value = "AcOperatorFuncQueConditionRequest",name = "传入的AcOperatorFuncQueConditionRequest类",required = true) AcOperatorFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acOperatorFuncService.queryFuncBehaveByOperatorApp(conditionRequest
                .getOperatorId(),conditionRequest.getAppId(),conditionRequest.getName()));
    }


    @ApiOperation(value = "根据姓名模糊查询应用和操作员下已有的功能和行为和所有的功能行为",notes = "传入AcOperatorFuncQueConditionRequest类作为参数")
    @PostMapping("/queryFuncBehaveFilterByOperatorApp")
    public ResultVO queryFuncBehaveFilterByOperatorApp (@RequestBody @Validated({AcOperatorFuncQueConditionRequest.filterByOperatorApp.class}) @ApiParam(value = "AcOperatorFuncQueConditionRequest",name = "传入的AcOperatorFuncQueConditionRequest类",required = true) AcOperatorFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acOperatorFuncService.getFilterFuncBehaveByRole(conditionRequest.getOperatorId(),conditionRequest.getName()));
    }


    @ApiOperation(value = "查询应用下所有功能和操作员下已有功能",notes = "传入AcOperatorFuncQueConditionRequest类作为参数")
    @PostMapping("/queryFuncByOperator")
    public ResultVO queryFuncByOperator (@RequestBody @Validated @ApiParam(value = "AcOperatorFuncQueConditionRequest",name = "传入的AcOperatorFuncQueConditionRequest类",required = true) AcOperatorFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acOperatorFuncService.queryFuncByOperator(conditionRequest));
    }


    @ApiOperation(value = "查询功能下所有行为和操作员下已有行为",notes = "传入AcOperatorFuncQueConditionRequest类作为参数")
    @PostMapping("/queryBehaveByOperator")
    public ResultVO queryBehaveByOperator (@RequestBody @Validated @ApiParam(value = "AcOperatorFuncQueConditionRequest",name = "传入的AcOperatorFuncQueConditionRequest类",required = true) AcOperatorFuncQueConditionRequest conditionRequest){
        return ResultVO.success("查询成功",acOperatorFuncService.queryBehaveByOperator(conditionRequest));
    }


    @ApiOperation(value = "批量新增和删除操作员功能",notes = "传入AcOperatorBatchAddDeleteRequest类作为参数")
    @OperateLog(type = OperateType.ADD,desc = "批量新增操作员功能信息")
    @PostMapping("/batchAdd")
    public ResultVO batchAddClear(@RequestBody @Validated @ApiParam(value = "AcOperatorBatchAddDeleteRequest",name = "传入的AcOperatorBatchAddDeleteRequest类",required = true) AcOperatorBatchAddDeleteRequest batchRequest){
        acOperatorFuncService.batchAddAndDelete(batchRequest.getOperatorGuid(),batchRequest.getDataList());
        return ResultVO.success("处理成功");
    }
}

